package com.healthbox.notifications.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.healthbox.notifications.commom.Result;
import com.healthbox.notifications.entity.HealthState;
import com.healthbox.notifications.entity.ReportInfo;
import com.healthbox.notifications.mapper.HealthStateMapper;
import com.healthbox.notifications.mapper.ReportInfoMapper;
import com.healthbox.notifications.service.ReportInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chs
 * @since 2022-05-12
 */
@Service
public class ReportInfoServiceImpl extends ServiceImpl<ReportInfoMapper, ReportInfo> implements ReportInfoService {

    @Resource
    private ReportInfoMapper reportInfoMapper;
    @Resource
    private HealthStateMapper healthStateMapper;

    /**
     * 填写报备信息
     * 函数实现
     * @author chs
     */
    @Override
    public Result addReportInfo(ReportInfo reportInfo,String phone) {
        if(reportInfo.getPhoneNum().equals(phone)){
            //前置判断今天是否已经填报过健康信息
            QueryWrapper<ReportInfo> qw = new QueryWrapper<>();
            qw
                    .eq("date", reportInfo.getDate());
            ReportInfo temp = reportInfoMapper.selectOne(qw);

            if(qw==null){
                reportInfoMapper.insert(reportInfo);

                //当体温大于37.3时，为发烧状态，状态转换为中风险
                if(reportInfo.getTemperature()>37.3){
                    QueryWrapper<HealthState> queryWrapper = new QueryWrapper<>();
                    queryWrapper
                            .eq("phoneNum", reportInfo.getPhoneNum());
                    HealthState query = healthStateMapper.selectOne(queryWrapper);
                    query.setHealthstate(1);
                    healthStateMapper.updateById(query);
                }

                //当risk为1，即14天内核酸检测结果为阳性时，健康状态转换为高风险
                if(reportInfo.getRisk()==1){
                    QueryWrapper<HealthState> queryWrapper = new QueryWrapper<>();
                    queryWrapper
                            .eq("phoneNum", reportInfo.getPhoneNum());
                    HealthState query = healthStateMapper.selectOne(queryWrapper);
                    query.setHealthstate(2);
                    healthStateMapper.updateById(query);
                }
                else{
                    //否则，当trip或contactor为1，即14天内途经中高风险地区或接触过中高风险人员时，健康状态转换为中风险
                    if(reportInfo.getTrip()==1 || reportInfo.getContactor()==1){
                        QueryWrapper<HealthState> queryWrapper = new QueryWrapper<>();
                        queryWrapper
                                .eq("phoneNum", reportInfo.getPhoneNum());
                        HealthState query = healthStateMapper.selectOne(queryWrapper);
                        query.setHealthstate(1);
                        healthStateMapper.updateById(query);
                    }
                }

                return Result.succeed(reportInfo);
            }
            else{
                return Result.fail("用户今日已经填报健康信息");
            }


        }
        else{
            return Result.fail("403","没有权限操作");
        }



    }

    /**
     * 查看历史报备信息
     * 函数实现
     * @author chs
     */
    @Override
    public Result getReportInfoList(String phone) {
            List<ReportInfo> reportInfoList = reportInfoMapper.selectList(new QueryWrapper<ReportInfo>().eq("phoneNum", phone));
            return Result.succeed(reportInfoList);

    }
}
